package com.wsd.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.idf.domain.ManagerDTO;
import com.idf.service.IManagerService;
import com.jwts.common.JwtClaims;
import com.wsd.common.Result;
import com.wsd.constant.ResultCode;
import com.wsd.service.AdminService;
import com.wsd.serviceImpl.AdminServiceImpl;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author zhangqizeng
 * 管理员各种操作接口
 */
@Controller
public class AdminController extends BaseController{


    @Autowired
    private AdminServiceImpl adminService;

    @RequestMapping(value = "/quickLoan/doLogin",method = RequestMethod.POST)
    @ResponseBody
    public Result doLogin(HttpServletRequest request,HttpServletResponse response,
                          @Param(value = "notesId") String notesId,
                          @Param(value = "password") String password){
        Result result = new Result();//参数命名
        //检查参数
        if(StringUtils.isEmpty(notesId) || StringUtils.isEmpty(password)){
            result.setResultCode(ResultCode.PARAM_IS_BLANK);
            return result;
        }

        result = changeResult(adminService.adminLogin(notesId,password));

        //idf验证成功
        if(result.getResultCode() == ResultCode.SUCCESS.getCode()){
            //设置token
            setToken(response,notesId);

            result.setResultCode(ResultCode.LOGIN_VAILDATE_SUCCESS);
        }else{
            //登录验证失败
            result.setResultCode(ResultCode.LOGIN_VAILDATE_FAILURE);
        }

        return result;
    }

    /**
     * 用户注销
     * @param response
     * @return
     */
    @RequestMapping(value = "/quickLoan/doLogout", method = RequestMethod.POST)
    @ResponseBody
    public Result doLogout(HttpServletResponse response){
        Result result = new Result();

        //移除token
        response.setHeader("Authorization",null);

        result.setResultCode(ResultCode.SUCCESS);

        return result;
    }

    /**
     * 新增管理员
     * @param request
     * @param notesId
     * @return
     */
    @RequestMapping(value = "/quickLoan/setAdmin",method = RequestMethod.POST)
    @ResponseBody
    public Result setAdminBySpAdmin(HttpServletRequest request, @Param(value = "notesId") String notesId){
        Result result = new Result();
        //检查参数
        if(StringUtils.isEmpty(notesId)){
            result.setResultCode(ResultCode.PARAM_IS_BLANK);
            return result;
        }
        //修改：从jwt中获取操作的管理员Notes
        JwtClaims claims = (JwtClaims) request.getAttribute("Claims");
        result = changeResult(adminService.insertAdmin(notesId,claims.get("userId").toString()));
        return result;
    }

    @RequestMapping(value = "/quickLoan/removeAdmin",method = RequestMethod.POST)
    @ResponseBody
    public Result removeAdmin(HttpServletRequest request, @Param(value = "notesId") String notesId){
        Result result = new Result();
        //检查参数
        if(StringUtils.isEmpty(notesId)){
            result.setResultCode(ResultCode.PARAM_IS_BLANK);
            return result;
        }
        //修改：从jwt中获取操作的管理员Notes
        JwtClaims claims = (JwtClaims) request.getAttribute("Claims");
        result = changeResult(adminService.removeAdmin(notesId,claims.get("userId").toString()));
        return result;
    }

    @RequestMapping(value = "/quickLoan/getAdminsInfo",method = RequestMethod.GET)
    @ResponseBody
    public Result getAdminsInfo(HttpServletRequest request){
        Result result = new Result();

        result = changeResult(adminService.listAdmin());
        return result;
    }

}
